// pages/myAddressEdit/myAddressEdit.js
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    address_id: null,
    region: ['广东省', '广州市', '海珠区'],
    customItem: '全部',
    username: '',
    mobile: '',
    address_detail: '',
    isEdit: false
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    console.log(e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  bindKeyName: function (e) {
    this.setData({
      username: e.detail.value
    })
  },
  bindKeyMobile: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  bindKeyDetailed: function (e) {
    this.setData({
      address_detail: e.detail.value
    })
  },
  submitFun: function () {
    if (this.data.isEdit) {
      //变更
      let that = this;
      app.get('User/useraddressaction', {
        address_id: this.data.address_id,
        userName: this.data.username,
        telNumber: this.data.mobile,
        provinceName: this.data.region[0],
        cityName: this.data.region[1],
        countyName: this.data.region[2],
        detailInfo: this.data.address_detail,
        type: '2' //1:新增 2:修改 3:删除
      }, function (res) {
        if (res.code == 200) {
          wx.showToast({
            title: res.message,
            duration: 1000,
            success: function () {
              wx.navigateBack({
                delta: 1
              })
              // wx.navigateTo({
              //   url: '../myAddress/myAddress',
              // })
            }
          })
        }
      })
    } else {
      // 新增
      let that = this;
      app.get('User/useraddressaction', {
        userName: this.data.username,
        telNumber: this.data.mobile,
        provinceName: this.data.region[0],
        cityName: this.data.region[1],
        countyName: this.data.region[2],
        detailInfo: this.data.address_detail,
        type: '1' //1:新增 2:修改 3:删除
      }, function (res) {
        if (res.code == 200) {
          wx.showToast({
            title: res.message,
            duration: 1000,
            success: function() {
              wx.navigateBack({
                delta: 1
              })
              // wx.navigateTo({
              //   url: '../myAddress/myAddress',
              // })
            }
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.address_id) {
      this.setData({
        region: options.city.split(','),
        username: options.username,
        mobile: options.mobile,
        address_detail: options.address_detail,
        address_id: options.address_id,
        isEdit: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})